//
//  CollectionFoldersViewController.m
//  OneCreditTreasure
//
//  Created by iOS Tedu on 16/8/29.
//
//

#import "CollectionFoldersViewController.h"
#import "CFOrderDetailViewController.h"
#import "CFTableViewCell.h"
#import "CFHeaderView.h"
#import "myCarModel.h"

@interface CollectionFoldersViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIBarButtonItem *rightBtn;

@property (nonatomic, strong) NSMutableArray *models;
@property (nonatomic, strong) NSMutableArray *selectedModels;
@end

@implementation CollectionFoldersViewController
#pragma mark - UITableView dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.models.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

kRemobeCellSeparator
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CFTableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    Cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    myCarModel *model = self.models[indexPath.row];
    Cell.leftLB.text = [NSString stringWithFormat:@"贷款额度\n\n%@", model.totalPayment];
    Cell.centerLB.text = [NSString stringWithFormat:@"首付\n\n%@", model.firstPay];
    Cell.rightLB.text = [NSString stringWithFormat:@"月供\n\n%@", model.payment];
    
    return Cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleInsert | UITableViewCellEditingStyleDelete;
}

#pragma mark - UITableView delegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    CFHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    myCarModel *model = self.models[section];
    view.leftLB.text = model.carName;
    view.rightLB.text = (model.carType == YDBCarTypeNewCar ? @"新车" : @"二手车");
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.isEditing) {
        [self.selectedModels addObject:self.models[indexPath.section]];
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    } else {
        CFOrderDetailViewController *vc = [CFOrderDetailViewController new];
        vc.model = self.models[indexPath.section];
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.isEditing) {
        [self.selectedModels removeObject:self.models[indexPath.section]];
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    }
}

#pragma mark - 生命周期 life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init {
    if (self = [super init]) {
        _rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"管理" style:UIBarButtonItemStylePlain target:self action:@selector(manageCollectionFolders:)];
        self.navigationItem.rightBarButtonItem = _rightBtn;
        self.title = @"收藏夹";
    }
    return self;
}

#pragma mark - 方法 methods
- (void)manageCollectionFolders:(UIBarButtonItem *)sender {
    if (!self.tableView.isEditing) {
        self.tableView.editing = !self.tableView.isEditing;
        self.rightBtn.title = self.selectedModels.count == 0 ? @"取消" : @"删除";
    } else {
        self.rightBtn.title = @"管理";
        if (self.selectedModels.count != 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"删除选中项" message:@"删除后不可恢复,您确认要删除吗?" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.models removeObjectsInArray:self.selectedModels];
                [self.selectedModels removeAllObjects];
                [self.tableView reloadData];
                self.tableView.editing = !self.tableView.isEditing;
            }];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.selectedModels removeAllObjects];
                self.tableView.editing = !self.tableView.isEditing;
            }];
            [alert addAction:confirm];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:nil];
        } else {
            self.tableView.editing = NO;
        }
    }

}

#pragma mark - 懒加载 lazy loading
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] init];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(0);
        }];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_tableView registerClass:[CFTableViewCell class] forCellReuseIdentifier:@"Cell"];
        [_tableView registerClass:[CFHeaderView class] forHeaderFooterViewReuseIdentifier:@"headerView"];
        
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (NSMutableArray *)models {
    if(_models == nil) {
        _models = [[myCarModel demoData] mutableCopy];
    }
    return _models;
}

- (NSMutableArray *)selectedModels {
    if(_selectedModels == nil) {
        _selectedModels = [[NSMutableArray alloc] init];
    }
    return _selectedModels;
}

@end
